<?php

use Biz\model\ShopFavorite as mShopFavorite;
use Biz\service\ShopGoodsOperator;
use Doraemon\cache\Queue;
use Doraemon\service\MemberRoleTag;
use Doraemon\tools\ActionLog as tActionLog;
use Doraemon\tools\HhzClient as tHhzClient;
use Doraemon\tools\PointConfig;
use Biz\cache\activity\SuperBuyerPlanetObj;
use Biz\model\activity\SuperBuyPlanet;
use Biz\model\activity\DigTreasure;
use hhz\constant\redis\redisKeys;
use Hhz\Redis\otherRedis;



class FavAction extends ActionPlugin
{

    const USER_LEVEL = self::LEVEL_REGISTER;

    const ACTION_TYPE = tActionLog::BIZ_GOODS_FAV;

    const DEFAULT_LOGIN_COPY_WRITING = "登录后\n收藏心愿好物";

    public function main()
    {
        $goodsIds = $this->getRequest()->getPost('goods_id', '');
        $objIds = $this->getRequest()->getPost('obj_id', '');
        $objType = $this->getRequest()->getPost('obj_type', mShopFavorite::TYPE_GOODS);
        $actFrom = $this->getRequest()->getPost('act_from', '');
        $statSign = $this->getRequest()->getPost('statSign', '');

        $_POST['act_params'] = $this->getRequest()->getPost('act_params', '');

        $goodsIds = array_filter(explode(',', $goodsIds));
        $objIds = array_filter(explode(',', $objIds));


        if ($goodsIds) {
            $objIds = $goodsIds;
        }
        if ($objType == mShopFavorite::TYPE_GOODS) {
            $this->pushLogAndQueue($objIds);
        }
        if ($objType == mShopFavorite::TYPE_WIKI) {
            $this->wikiPushLogAndQueue($objIds, $actFrom);
        }
        $appSource = 0;
        if (tHhzClient::getClientType() == tHhzClient::APPTYPE_PARAGRAPH_MINIPROGRAM) {
            $appSource = tHhzClient::APPTYPE_PARAGRAPH_MINIPROGRAM;
        } else if (tHhzClient::getClientType() == tHhzClient::APPTYPE_DESIGNER_FUNENG_MINIPROGRAM) {
            $appSource = tHhzClient::APPTYPE_DESIGNER_FUNENG_MINIPROGRAM;
        }

        $uid = $this->uid;
        foreach ($objIds as $objId) {
            try {
                ShopGoodsOperator::favorite($this->uid, $objId, $objType, $appSource);
            } catch (Exception $e) {
                if ($objType == mShopFavorite::TYPE_GOODS) {
                    continue;
                }
            } finally {
                \Doraemon\tools\Log::debug('用户收藏行为', compact('uid', 'objType', 'objId', 'appSource'));
            }
            // actionlog埋点，整理人生活动
            if ($actFrom == 'arrange_life') {
                tActionLog::log($this->uid, [
                    'uid' => $this->uid,
                    'spu_id' => $objId,
                ], tActionLog::ARRANGE_LIFE);
            }

        }
        $obj_id = array_shift($objIds);
        if ($objType == mShopFavorite::TYPE_WIKI && !empty($obj_id)) {
            //积分操作-商业-收藏商品增加积分
            \Doraemon\amqp\CommunityQueue::insertQueue(PointConfig::MQ_QUEUE_URL, [
                'event_name' => PointConfig::POINT_TYPE_BUSINESS_COLLECT_GOODS,
                'obj_id' => $obj_id,
                'uid' => $uid,
                'type' => PointConfig::POINT_AWARD
            ], \Doraemon\amqp\CommunityQueue::QUEUE_COMMUNITY);
        }

//        $sConstKey = redisKeys::KEY_B_OTHER_SUPER_BUYER_PLANET_WIKI;
//        $redisPlanet = new otherRedis($sConstKey);
//        $cacheKey = $sConstKey .'_'.$this->uid;
//        $redisPlanet->SISMEMBER($cacheKey,$obj_id);

        //超会买星球加加分
        if( $statSign &&  !SuperBuyerPlanetObj::IssetWiki($this->uid , $obj_id)){
            $statSignArr = json_decode($statSign, true);
            $ContentobjId = $statSignArr ? $this->getObjId($statSignArr) : '';
            $cache = SuperBuyerPlanetObj::GetObjByUid($this->uid);
//            $redis = new cacheRedis(redisKeys::KEY_B_CACHE_SUPER_BUYER_PLANET_OBJ);
//            $cache = $redis->getCache($this->uid);
            if($ContentobjId && $cache == $ContentobjId){
                $info =   SuperBuyPlanet::getByUid($this->uid);
                if($info && is_array($info)){
                    if($info['fav_wiki_cnt'] < 5){
                        if($info['fav_wiki_cnt'] + 1 < 5){
                            $point = 3;
                        }elseif($info['fav_wiki_cnt'] + 1 == 5){
                            $point = 100;
                        }
                        \Doraemon\amqp\CommunityQueue::insertQueue(PointConfig::MQ_QUEUE_URL, [
                            'event_name' => PointConfig::SUPER_BUY_STAR_DIGGING_TREASURE,
                            'obj_id' => 0,
                            'uid' => $this->uid,
                            'point' => $point,
                            'type' => PointConfig::POINT_AWARD
                        ], \Doraemon\amqp\CommunityQueue::QUEUE_COMMUNITY);
                    }
                    SuperBuyPlanet::updateData(['uid' => $this->uid , 'fav_wiki_cnt' => $info['fav_wiki_cnt'] +1]);
                    DigTreasure::inserData(['uid' => $this->uid , 'wiki_id' => $obj_id]);
                    SuperBuyerPlanetObj::setWiki($this->uid ,$obj_id);

//                    $redisPlanet->sadd($cacheKey,$obj_id);

                }
            }
        }
        //超会买星球加加分
        $this->setSuccess(null, self::API_CODE_SUCCES);
    }

    private function pushLogAndQueue($objIds)
    {
        $firstFavs = mShopFavorite::select('*', [
            'uid' => $this->uid,
            'obj_id' => $objIds,
            'obj_type' => mShopFavorite::TYPE_GOODS,
            'status' => mShopFavorite::STATUS_DEL,
        ]);
        $firstFavs = array_column($firstFavs, null, 'obj_id');

        foreach ($objIds as $objId) {
            tActionLog::log($this->uid, [
                'uid' => $this->uid,
                'spu_id' => $objId,
                'first_fav' => isset($firstFavs[$objId]) ? 0 : 1
            ], tActionLog::BIZ_GOODS_FAV);
        }

        Queue::insertQueue('queue/UserRole/SaveRoleTag', [
            'vid' => $this->vid,//vid
            'tag_id' => MemberRoleTag::ROLE_TAG_FAV_GOODS,//分群id
            'value' => 2,//对应的值
            'uid' => $this->uid,
        ], Queue::QUEUE_ROLE_TAG);
    }

    private function wikiPushLogAndQueue($objIds, $actFrom = '')
    {
        $firstFavs = mShopFavorite::select('*', [
            'uid' => $this->uid,
            'obj_id' => $objIds,
            'obj_type' => mShopFavorite::TYPE_WIKI,
            'status' => mShopFavorite::STATUS_DEL,
        ]);
        $firstFavs = array_column($firstFavs, null, 'obj_id');

        foreach ($objIds as $objId) {
            tActionLog::log($this->uid, [
                'uid' => $this->uid,
                'wiki_id' => $objId,
                'first_fav' => isset($firstFavs[$objId]) ? 0 : 1,
                'act_form' => $actFrom
            ], tActionLog::BIZ_WIKI_FAV);
        }

//        Queue::insertQueue('queue/UserRole/SaveRoleTag', [
//            'vid' => $this->vid,//vid
//            'tag_id' => MemberRoleTag::ROLE_TAG_FAV_GOODS,//分群id
//            'value' => 2,//对应的值
//            'uid' => $this->uid,
//        ], Queue::QUEUE_ROLE_TAG);
    }

    private function getObjId($statSignArr)
    {
        $params = $statSignArr['ps'] ?? [];
        $objId = $params['obj_id'] ?? '';
        if (!$objId) {
            $objId = $params['relate_id'] ?? '';
        }
        return $objId;
    }


}